package chapter01.part8;

import chapter01.part2.CreateStream;

import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 处理完流后 把流收集起来操作
 * 比如打印 或者 转换成其他容器
 * 
 * @author insight 
 * @since 2021/7/19
 */
public class CollectStream {
    public static void main(String[] args) {
        Supplier<Stream<String>> words = CreateStream.getWordsStream();

        // foreach print
        words.get().forEach(System.out::println);

        // to collections
        // array
        Object[] objects = words.get().toArray();
        String[] strings = words.get().toArray(String[]::new);

        // collect
        // list
        List<String> collect = words.get().collect(Collectors.toList());
        // Control the type of the collection
        ArrayList<String> collect1 =
                words.get().collect(Collectors.toCollection(ArrayList::new));
        // connect element
        String collect2 = words.get().collect(Collectors.joining(" "));
        // cal
        IntSummaryStatistics summary =
                words.get().collect(Collectors.summarizingInt(String::length));
        System.out.println(summary.getAverage());
        System.out.println(summary.getMax());
    }
}